package com.example.hello.service;

import com.example.hello.controller.UserDto;
import com.example.hello.dao.LogDao;
import com.example.hello.dao.UserDao;
import com.example.hello.entity.Log;
import com.example.hello.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author sjw
 * @className HelloService
 * @date 2022/5/19 9:32
 * @description: TODO
 */
@Service
public class HelloService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private LogDao logDao;

    public long hello(String id) {
        long tId = Thread.currentThread().getId();
        System.out.println(id + " - " + tId + " - hello");
        Optional<User> optionalUser = userDao.findById(id);

        optionalUser.ifPresent(user -> System.out.println(user.getName()));

        User user = new User();
        user.setName("springboot");
        user.setAge(19);
        userDao.save(user);
        System.out.println(user.getId());
//        new Thread(new HelloThread()).start();
        return tId;
    }


    @Transactional(rollbackFor = Exception.class)
    public String user(UserDto userDto) throws Exception {
        User user = null;
        if (userDto.getId() != null) {
            Optional<User> optionalUser = userDao.findById(userDto.getId());
            if (!optionalUser.isPresent()) return null;
            user = optionalUser.get();
        } else {
            user = new User();
        }
        user.setName(userDto.getName());
        user.setAddress(userDto.getAddress());
        user.setVersion(user.getVersion() + 1);
        System.out.println(" save begin");
        userDao.save(user);
        System.out.println(" save end");
        sleep(5);
        Log log = new Log();
        log.setUserId(user.getId());
        logDao.save(log);
        sleep(5);
        if (user.getAge() == 18) throw new Exception();
        System.out.println(" tx end");
        return user.getId();
    }

//    @Transactional(rollbackFor = Exception.class)
    public String user_noTx(UserDto userDto) throws Exception {
        User user = null;
        if (userDto.getId() != null) {
            System.out.println("find begin");
            Optional<User> optionalUser = userDao.findById(userDto.getId());
            System.out.println("find end");
            if (!optionalUser.isPresent()) return null;
            user = optionalUser.get();
        } else {
            user = new User();
        }
        user.setAge(userDto.getAge());
        user.setAddress(userDto.getAddress());
        user.setVersion(user.getVersion() + 1);
        System.out.println("no save begin");
//        userDao.save(user);
        System.out.println("no save end");
        mongoTemplate.updateMulti(new Query( Criteria.where("id").is(userDto.getId())),
                Update.update("age",userDto.getAge())
                ,User.class);
        mongoTemplate.
        return user.getId();
    }


    private class HelloThread implements Runnable {
        @Override
        public void run() {
            while (true) {
                System.out.println("HelloThread - " + Thread.currentThread().getId());
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void sleep(int s) throws InterruptedException {
        for (int i = 0; i < s; i++) {
            Thread.sleep(1000);
        }
    }

}
